package com.code1030.studentsystem;

import java.util.Scanner;
import java.util.ArrayList;

public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> studentsList = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出学生管理系统 ");
            System.out.println("请输入你的选择");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(studentsList);
                case "2" -> deleteStudent(studentsList);
                case "3" -> updateStudent(studentsList);
                case "4" -> queryStudent(studentsList);
                case "5" -> {
                    System.out.println("退出学生管理系统");
//                    break loop;
//                    停止虚拟机运行
                    System.exit(0);
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入学生的id");
            id = sc.next();
            boolean idBol = haveIdStudent(list, id);
            if (idBol) {
                System.out.println("该id已存在 请重新输入学生的id");
            } else {
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生的地址");
        String address = sc.next();
        Student stu = new Student(id, name, age, address);
        list.add(stu);
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入要删除的学生id");
        Scanner sc = new Scanner(System.in);
        String delId = sc.next();
        boolean idBol = haveIdStudent(list, delId);
        if (idBol) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(delId)) {
                    list.remove(i);
                    System.out.println("删除成功");
                    break;
                }
            }
        } else {
            System.out.println("该id不存在 请重新选择你要进行的操作");
        }
    }

    public static void updateStudent(ArrayList<Student> list) {
        System.out.println("请重新输入学生的姓名");
        Scanner sc = new Scanner(System.in);
        String updateId = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(updateId)) {
                System.out.println("请重新输入学生的姓名");
                String name = sc.next();
                System.out.println("请重新输入学生的年龄");
                int age = sc.nextInt();
                System.out.println("请重新输入学生的地址");
                String address = sc.next();
                Student stu = new Student(updateId, name, age, address);
                list.set(i, stu);
                System.out.println("修改成功");
                break;
            }
        }
    }

    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息");
        } else {
            System.out.println("id\tname\tage\taddress");
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
            }
        }
    }

    public static boolean haveIdStudent(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }
}
